//
//  DraftView.m
//  Knowbox
//
//  Created by yestem on 14-11-15.
//  Copyright (c) 2014年 knowin. All rights reserved.
//

#import "SimDrawView.h"
#import "SimDrawViewModel.h"

@interface InnerDrawView : UIView

@property (nonatomic, strong) SimDrawViewModel *drawModel;
@property (nonatomic) CGRect clipRect;
@property (nonatomic, strong) UIColor *strokeColor;

@end

@implementation InnerDrawView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect{    
    if (!CGRectIsEmpty(_clipRect)) {
        CGContextRef curContext = UIGraphicsGetCurrentContext();
        CGContextClipToRect(curContext, _clipRect);
    }
    
    if (!_drawModel.isClear) {
        [_strokeColor setStroke];
        for (UIBezierPath *path in _drawModel.pathArray){
            [path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
        }
    }
}

@end

@interface SimDrawView()
{
    UIBezierPath *_bezierPath;
    InnerDrawView *_innerView;
}

@end

@implementation SimDrawView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor clearColor];
        _strokeColor = [UIColor blackColor];
        _strokeWidth = 2;
        _drawModel = [[SimDrawViewModel alloc] init];
        _innerView = [[InnerDrawView alloc] initWithFrame:self.bounds];
        [self addSubview:_innerView];
    }
    return self;
}


- (void)updateDraw
{
    if (_drawModel.preImage) {
        self.image = nil;
    }
    _innerView.drawModel = self.drawModel;
    _innerView.strokeColor = self.strokeColor;
    _innerView.clipRect = self.clipRect;
    [_innerView setNeedsDisplay];
}


- (void)setDrawModel:(SimDrawViewModel *)model
{
    if (![_drawModel isEqual:model]) {
        _drawModel = model;
    }
    [self updateDraw];
}


- (BOOL)canClear
{
    if (_drawModel.pathArray.count > 0 && !_drawModel.isClear) {
        return YES;
    }
    
    if (self.image != nil) {
        return YES;
    }
    
    return NO;
}

- (BOOL)canRevocation
{
    if (_drawModel.pathArray.count > 0 || _drawModel.isClear) {
        return YES;
    }
    
    if (_drawModel.preImage) {
        return YES;
    }
    
    return NO;
}

- (BOOL)canReform
{
    if (_drawModel.deleArray.count > 0 && !_drawModel.isClear && _drawModel.preImage == nil) {
        return YES;
    }
    
    return NO;
}

- (void)addPoint:(CGPoint)point
{
    if (_drawModel.isClear) {
        [_drawModel.pathArray removeAllObjects];
        _drawModel.isClear = NO;
    }
    if (_drawModel.preImage) {
        _drawModel.preImage = nil;
    }
    
    if (!_bezierPath) {
        _bezierPath = [[UIBezierPath alloc] init];
        [_bezierPath setLineCapStyle:kCGLineCapRound];
        [_bezierPath setLineWidth:_strokeWidth];
        [_bezierPath setMiterLimit:0];
        
        [_bezierPath moveToPoint:point];
        [_drawModel.pathArray addObject:_bezierPath];
    }
    else{
        [_bezierPath addLineToPoint:point];
    }
    [self updateDraw];
}

- (void)finishPoint
{
    [_drawModel.deleArray removeAllObjects];
    
    if (_bezierPath) {
        _bezierPath = nil;
    }
    [self updateDraw];
    if (self.viewDidUpdate) {
        self.viewDidUpdate();
    }
}

#pragma mark --

//移除所有的线条并重绘
- (void)clear
{
    if (!_drawModel.isClear && _drawModel.pathArray.count > 0) {
        [_drawModel.deleArray removeAllObjects];
        _drawModel.isClear = YES;
        [self updateDraw];
    }
    else if(self.image != nil){
        _drawModel.preImage = self.image;
        self.image = nil;
    }
    if (self.viewDidUpdate) {
        self.viewDidUpdate();
    }
}

//撤销，将当前最后一条信息移动到删除数组里，方便恢复时调用
- (void)revocation
{
    if (_drawModel.preImage) {
        self.image = _drawModel.preImage;
        _drawModel.preImage = nil;
    }
    else if(_drawModel.isClear){
        _drawModel.isClear = NO;
    }
    else{
        if (_drawModel.pathArray.count > 0) {
            [_drawModel.deleArray addObject:[_drawModel.pathArray lastObject]];
            [_drawModel.pathArray removeLastObject];
        }
    }
    
    [self updateDraw];
    if (self.viewDidUpdate) {
        self.viewDidUpdate();
    }
}

//将删除线条数组里的信息，移动到当前数组，在主界面重绘
- (void)reform
{
    if ([_drawModel.deleArray count] > 0) {
        [_drawModel.pathArray addObject:[_drawModel.deleArray lastObject]];
        [_drawModel.deleArray removeLastObject];
     }
    
    [self updateDraw];
    if (self.viewDidUpdate) {
        self.viewDidUpdate();
    }
}

- (void)mergeImageForImage:(UIImage *)mergeImage
{
    UIImage *image = nil;
    if (_drawModel.pathArray.count > 0) {
        if (!CGRectIsEmpty(_clipRect)) {
            UIGraphicsBeginImageContextWithOptions(_clipRect.size, NO, mergeImage.scale);
            CGContextRef ctx = UIGraphicsGetCurrentContext();
            CGContextTranslateCTM(ctx, -_clipRect.origin.x, -_clipRect.origin.y);
            [self.layer renderInContext:ctx];
            image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }
        else{
            UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, mergeImage.scale);
            CGContextRef curContext = UIGraphicsGetCurrentContext();
            [self.layer renderInContext:curContext];
            image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }
        self.image = image;
        
        [_drawModel.pathArray removeAllObjects];
    }
    else{
        self.image = nil;
    }
    
    [_drawModel.deleArray removeAllObjects];
    [self updateDraw];
    
    if (self.viewDidUpdate) {
        self.viewDidUpdate();
    }
}


@end
